﻿package little.bunny;

import java.util.ArrayList;

import little.bunny.adapter.Nei_List_Adapter;
import little.bunny.bean.SortS;
import little.bunny.create_dh.CreateDaoHang;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnKeyListener;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class NeiListActivity extends Activity {
	private ListView myListView;
	private Button myButton,backButton;
	// /private ArrayList<SortS> getSortS_List=new ArrayList<SortS>();
	//private CreateDaoHang cdh = new CreateDaoHang();
	private String UrlTrue, url, moban, start, end;
	private int s, d;
	private ArrayList<SortS> myListSorts;
	private Handler myHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.nei_list_activity_layout);
		
		backButton=(Button)findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		
		
		// 获取string.xml中的资源文件
		UrlTrue = (String) getResources().getText(R.string.url);
		Intent myIntent = getIntent();
		url = myIntent.getStringExtra("url");
		moban = myIntent.getStringExtra("moban");
		start = myIntent.getStringExtra("start");
		s = myIntent.getIntExtra("startid", 1);
		end = myIntent.getStringExtra("end");
		d = myIntent.getIntExtra("endid", 1);
		myButton = (Button) findViewById(R.id.nei_list_title);
		myButton.setText(myIntent.getStringExtra("title"));
		myListView = (ListView) findViewById(R.id.nei_listView1);
		myListView.setVerticalScrollBarEnabled(false);
		// 创建进度条
		final ProgressDialog progressDialog = ProgressDialog.show(this, "请稍等",
				"数据加载中...", true);
		progressDialog.setOnKeyListener(new OnKeyListener() {
			
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_BACK){
					progressDialog.dismiss();
				}
				return false;
			}
		});
		// 创建线程
		new Thread() {
			public void run() {
				Looper.prepare();
				
				try {
					myListSorts = CreateDaoHang.getSortList(url, moban, start, s,
							end, d);
					if(myListSorts==null||myListSorts.size()==0){
						progressDialog.dismiss();
						new AlertDialog.Builder(NeiListActivity.this)
						.setTitle("网络信号异常")
						.setMessage("请查看网络连接，请稍后再试...")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										NeiListActivity.this.finish();
									}
								}).show();
					}else{
						myHandler.post(addValues);
					}
					
				} catch (Exception e) {
					// TODO: handle exception
					// System.out.println(e.getMessage());
					progressDialog.dismiss();
					System.out.println("NeiListActivity获取服务器传递数据时候，报错。。。。。。。。。。");
					new AlertDialog.Builder(NeiListActivity.this)
					.setTitle("网络信号异常")
					.setMessage("请查看网络连接，请稍后再试...")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									NeiListActivity.this.finish();
								}
							}).show();
					
				}
				progressDialog.dismiss();// 隐藏进度条
				Looper.loop();
			}
		}.start();

	}

	final Runnable addValues = new Runnable() {

		public void run() {
			try {
				// TODO Auto-generated method stub
				
				myListView.setAdapter(new Nei_List_Adapter(
						NeiListActivity.this, myListSorts));
				myListView.setOnItemClickListener(new OnItemClickListener() {
					
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						// String a="       ";
						Intent myIntent = new Intent(NeiListActivity.this,
								ContentActivity.class);
						myIntent.putExtra("Title", myListSorts.get(arg2)
								.getSortName().toString());
						// , , );
						myIntent.putExtra("start",
								"<div class=\"sub_zzjs\" align=\"left\">");
						myIntent.putExtra("end",
								"<td width=\"200\" align=\"right\" valign=\"top\">");
						///取消样式 标记
						myIntent.putExtra("moban",
								"&nbsp;|<!--[\\s\\S]*?-->|<STYLE>[\\s\\S]*?</STYLE>|<.*?>");
						
						/*"<a.*?>|</a>|<A.*?>|<.*?>|&nbsp;");*/
						myIntent.putExtra("Con", UrlTrue
								+ myListSorts.get(arg2).getUrl().toString());
						// .replaceFirst("\\s{1,}", "\n\n"));
						// a+cdh.getListContent(UrlTrue+myListSorts.get(arg2).getUrl().toString()).replaceAll("\\s{2,}",
						// "\n")
						startActivity(myIntent);
					}
				});
			} catch (Exception e) {
				// TODO: handle exception
				new AlertDialog.Builder(NeiListActivity.this)
						.setTitle("网络信号异常")
						.setMessage("请查看网络连接，请稍后再试...")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										System.exit(0);
									}
								}).show();
			}
		}

	};

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// land do nothing is ok
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			// port do nothing is ok
		}
	}

}
